package org.judo.server;

import java.io.File;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHttpContext;

public class WebAppServer
{
	private static Server testServer;
	private static Server consoleServer;
	private static String testServerPort;
	private static String consoleServerPort;
	
	public static synchronized void redeploy()
		throws Exception
	{
		System.out.println("Restarting Test Server at port: "+testServerPort);
		testServer.stop();
		testServer.start();
	}
	
	public static void main(String args[])
	{
		File f = new File("");
		System.out.println("Current Path: "+f.getAbsolutePath());
		
		try
		{
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(new File("server/config/server.xml"));
			Element root = doc.getRootElement();
			
			testServerPort = root.getChild("test-server").getAttribute("port").getValue();
			
			HotDeployer hotDeployer = new HotDeployer();
			hotDeployer.checkFiles();
			
			System.out.println("Starting Test Server at port: "+testServerPort);
	
			testServer = new Server();
			testServer.addListener(testServerPort);
			ServletHttpContext context = (ServletHttpContext)
			    testServer.getContext("/context");
			
			testServer.addWebApplication("webapp","server/webapp/");
			testServer.start();
			
			(new HotDeployer()).start();
		}
		catch(Throwable e)
		{
			e.printStackTrace();
			System.out.println("Error Starting Jetty Server: "+e);
			System.out.println("Hit Enter to quit...");
			try{System.in.read();}catch(Exception e2){}
		}
	}
}
